home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jentrymouse.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  5.3 KB  |  175 lines

  1. # jentrymouse.tcl - support for Entry mouse bindings
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5. # Essentially, call
  6. #   j:eb:basic_bind Entry   ---   for basic Entry bindings
  7. #   j:eb:emacs_bind Entry   ---   for Emacs-like Entry bindings
  8. #   j:eb:vi_bind Entry      ---   for Emacs-like Entry bindings
  9. # (the emacs_bind and vi_bind procedures include the basic_bind 
  10. # procedures; you don't have to call both.)
  11.  
  12. # TO DO:
  13. # ^L
  14. # sentence-manipulation stuff
  15. # case change commands, transposition commands
  16. # commands to do with mark?
  17. # word deletion - fix to use buffer
  18. # generalise movement to copying-to-cutbuffer and deletion
  19. # IMPROVE ENTRY BINDINGS
  20. # literal-insert for entry
  21.  
  22. ######################################################################
  23. # global variables:
  24. #
  25. global J_PREFS env
  26. if {! [info exists J_PREFS(typeover)]} {set J_PREFS(typeover) 1}
  27. #
  28. ######################################################################
  29.  
  30. ######################################################################
  31. ######################################################################
  32. ##  MOUSE BINDINGS
  33. ######################################################################
  34. ######################################################################
  35.  
  36. ######################################################################
  37. # routines for dragging the selection - fix timing problems
  38. ######################################################################
  39.  
  40. # j:emb:move_sel W x y t - move/start selection, clearing existing selection
  41.  
  42. proc j:emb:move_sel { W x y t } {
  43.   global j_teb
  44.   
  45.   $W icursor @$x
  46.   $W select from @$x
  47.   if {[lindex [$W config -state] 4] == "normal"} {focus $W}
  48.   
  49.   set j_teb(eselstart,x) $x
  50. }
  51.  
  52. # j:emb:continue_sel W x y t - drag out a selection if enough time has passed
  53.  
  54. proc j:emb:continue_sel { W x y t } {
  55.   global j_teb
  56.   
  57.   set xdiff [expr {abs($x - $j_teb(eselstart,x))}]
  58.   if {$xdiff < 3} {
  59.     return
  60.   }
  61.   
  62.   $W select to @$x
  63. }
  64.  
  65. ######################################################################
  66. # routines to let scanning and pasting double-up on the same button
  67. # based on code by Tom Phelps <phelps@cs.berkeley.edu>
  68. ######################################################################
  69.  
  70. # j:emb:start_scan_or_paste W x y t - start a drag or paste, recording
  71. #   current location and time so we can later decide whether to paste or drag
  72. # BIND TO <ButtonPress-N>
  73. proc j:emb:start_scan_or_paste { W x y t } {
  74.   global j_teb
  75.   $W scan mark $x
  76.   set j_teb(scanpaste_time) $t
  77.   set j_teb(scanpaste_paste) 1
  78. }
  79.  
  80. # j:emb:continue_scan W x y t - scan the entry, and mark the fact that
  81. #   we're scanning, not pasting.
  82. # BIND TO <BN-Motion>
  83. proc j:emb:continue_scan { W x y t } {
  84.   global j_teb
  85.   $W scan dragto $x
  86.   set j_teb(scanpaste_paste) 0
  87. }
  88.  
  89. # j:emb:end_scan_or_paste W x y t - if we haven't been scanning, and it's
  90. #   been less than 500ms since button-down, paste selection
  91. # BIND TO <ButtonRelease-N>
  92. proc j:emb:end_scan_or_paste { W x y t } {
  93.   global j_teb
  94.   if {$j_teb(scanpaste_paste) &&
  95.       [expr {$t-$j_teb(scanpaste_time)}] < 500} {
  96.     focus $W
  97.     j:eb:paste_selection $W
  98.   }
  99. }
  100.  
  101. ######################################################################
  102. # j:emb:sel_word W - select current word (at insert point)
  103. # hacked from tk_entryBackword in entry.tcl
  104. ######################################################################
  105.  
  106. proc j:emb:sel_word { W args } {
  107.   set string [$W get]
  108.   set length [string length $string]
  109.   set curs [expr [$W index insert]-1]
  110.   if {$curs < 0} return
  111.   for {set x $curs} {$x > 0} {incr x -1} {
  112.     if {[string first [string index $string $x] " \t"] >= 0} {
  113.       incr x
  114.       break
  115.     }
  116.     if {([string first [string index $string $x] " \t"] < 0)
  117.         && ([string first [string index $string [expr $x-1]] " \t"]
  118.         >= 0)} {
  119.       break
  120.     }
  121.   }
  122.   set start $x
  123.   for {set x $curs} {$x < $length} {incr x 1} {
  124.     if {([string first [string index $string $x] " \t"] < 0)
  125.         && ([string first [string index $string [expr $x+1]] " \t"]
  126.         >= 0)} {
  127.       break
  128.     }
  129.   }
  130.   set end $x
  131.   $W select from $start
  132.   $W select to $end
  133.   $W icursor $start
  134.   tk_entrySeeCaret $W
  135. }
  136.  
  137. # j:emb:sel_line W - select entire entry
  138. proc j:emb:sel_line { W args } {
  139.   $W select from 0
  140.   $W select to end
  141.   $W icursor 0
  142.   tk_entrySeeCaret $W
  143. }
  144.  
  145. ######################################################################
  146.  
  147. # j:eb:mouse_bind W - set up W (normally "Entry") for basic editing
  148. proc j:eb:mouse_bind { W args } {
  149.   global j_teb
  150.   
  151.   j:tk3 {                ;# bindings are superfluous in Tk 4
  152.     # mouse bindings for selection:
  153.     bind $W <1>            {j:emb:move_sel %W %x %y %t}
  154.     bind $W <B1-Motion>        {j:emb:continue_sel %W %x %y %t}
  155.     
  156.     bind $W <Double-1>        {j:emb:sel_word %W %x %y %t}
  157.     bind $W <Triple-1>        {j:emb:sel_line %W %x %y %t}
  158.     
  159.     # mouse bindings for scanning and pasting:
  160.     bind $W <2>            {j:emb:start_scan_or_paste %W %x %y %t}
  161.     bind $W <B2-Motion>        {j:emb:continue_scan %W %x %y %t}
  162.     bind $W <ButtonRelease-2>    {j:emb:end_scan_or_paste %W %x %y %t}
  163.     
  164.     # mouse bindings for scanning and pasting:
  165.     bind $W <3>            {j:emb:start_scan_or_paste %W %x %y %t}
  166.     bind $W <B3-Motion>        {j:emb:continue_scan %W %x %y %t}
  167.     bind $W <ButtonRelease-3>    {j:emb:end_scan_or_paste %W %x %y %t}
  168.   }
  169. }
  170.  
  171.  
  172.